package com.qf.fmcommodity.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmcommodity.user.entity.Product;
import com.qf.fmcommodity.user.entity.ProductImg;
import com.qf.fmcommodity.user.entity.ProductSku;
import com.qf.fmcommodity.user.mapper.ProductMapper;
import com.qf.fmcommodity.user.service.IProductImgService;
import com.qf.fmcommodity.user.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmcommodity.user.service.IProductSkuService;
import com.qf.fmcommodity.user.vo.ProSkuImgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author 苇名一心
 * @since 2023-02-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    IProductImgService iProductImgService;

    @Autowired
    IProductSkuService iProductSkuService;



    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Product> listRecommends() {

        //分页查询三条数据
        Page<Product> productPage = new Page<>(1,3);

        //排序
        productPage.addOrder(OrderItem.desc("create_time"));
        //执行分页查询
        page(productPage);

        //获取数据
        List<Product> productList = productPage.getRecords();


        //创建条件对象
        QueryWrapper<ProductImg> wrapper = new QueryWrapper<>();

        List<String> productIdList = productList.stream().map(Product::getProductId)// 把原集合中的每个元素进行类型转换
                .collect(Collectors.toList());//在转换成list

        wrapper.in("item_id",productIdList);

        //查询数据
        List<ProductImg> list = iProductImgService.list(wrapper);

        Map<String, List<ProductImg>> collectMap = list.stream().collect(Collectors.
                groupingBy(ProductImg::getItemId));

        productList.forEach(product -> product.setImgs(collectMap.get(product.getProductId())));



        return productList;
    }

    @Override
    public ProSkuImgVo detailInfo(Integer id) {

        //根据id查询数据product
        Product product = getOne(new QueryWrapper<Product>().eq("product_id", id));

        //拿商品图片
        List<ProductImg> productImgs = iProductImgService.list(new QueryWrapper<ProductImg>().eq("item_id", id));

        product.setImgs(productImgs);

        List<ProductSku> productSkus = iProductSkuService.list(new QueryWrapper<ProductSku>().eq("product_id", id));


        ProSkuImgVo proSkuImgVo = new ProSkuImgVo();

        proSkuImgVo.setProduct(product);
        proSkuImgVo.setProductImgs(productImgs);
        proSkuImgVo.setProductSkus(productSkus);



        return proSkuImgVo;
    }
}
